// pages/msg/msg.js
const app = getApp();
import config from "../../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 我的管家
    manager: {},

    // 消息列表
    msg: [{
      icon: "../../../image/msgInfo-assisant.png",
      title: "订单助手",
      detail: "",
      time: "",
      count: 0,
      url: "../../msg/msgList?id=1",
    }, {
      icon: "../../../image/msgInfo-pro.png",
      title: "菇咚公告",
      detail: "",
      time: "",
      count: 0,
      url: "../../msg/msgList?id=2",
    }, {
      icon: "../../../image/msginfo-action.png",
      title: "活动消息",
      detail: "",
      time: "",
      count: 0,
      url: "../../msg/msgList?id=3",
    }],
    star: [],
    //订单助手
    orderHelper: {},
    //菇咚公告
    gdNotice: {},
    //活动公告
    actNotice: {},
    //即时消息
    gdMessage: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    app.editTabBar();

    var user = wx.getStorageSync('user');
    var score = user.housekeeper.score;
    var star = [];
    for (var i = 0; i < score; i++) {
      star.push(i);
    }
    this.setData({
      manager: user.housekeeper,
      star: star
    })
    //测试
    // wx.setStorageSync("webviewUrl", "https://app.longchg.com/gudongcar/newsContent.action?contentId=6");



  },
  loadData: function() {
    var that = this;
    //订单助手最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 10000,
        typeId: 1,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              orderHelper: obj
            })
          } else {
            let arr = [];
            that.setData({
              orderHelper: res.data.data[0]
            })
          }

          if (res.data.data != null && res.data.data.length > 0) {
            that.setData({
              'msg[0].count': res.data.data.length,
              'msg[0].detail': that.data.orderHelper.ncontent,
              'msg[0].time': that.data.orderHelper.ntime,
            })
          } else {
            that.setData({
              'msg[0].count': 0,
              'msg[0].detail': "暂无最新消息",
            })
          }

        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })

    //菇咚公告最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        typeId: 2,
        size: 10000,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              gdNotice: obj
            })
          } else {
            let arr = [];
            that.setData({
              gdNotice: res.data.data[0]
            })
          }
          if (res.data.data != null && res.data.data.length > 0) {
            that.setData({
              'msg[1].count': res.data.data.length,
              'msg[1].detail': that.data.gdNotice.ncontent,
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })

    //活动消息最新消息
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        typeId: 3,
        size: 10000,
        flag: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {
            var obj = {
              ntype: {
                nname: "暂无消息"
              }
            }
            that.setData({
              actNotice: obj
            })
          } else {
            let arr = [];
            that.setData({
              actNotice: res.data.data[0]
            })
          }
          if (res.data.data != null && res.data.data.length > 0) {
            that.setData({
              'msg[2].count': res.data.data.length,
              'msg[2].detail': that.data.actNotice.ncontent,
            })
          }

        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.loadOffData();
    that.loadData();
    that.addListener();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  addListener: function() {
    var that = this;
    console.log("添加监听");
    app.globalData.callback = function(res) {
      console.log('收到服务器内容3：' + res.data)
      wx.showTabBarRedDot({
        index: 1,
      })
      that.loadOffData();
    }

  },
  loadOffData: function() {
    var that = this;
    //即时通讯
    wx.request({
      url: base.chatOffLineMessageList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        start: 0,
        size: 10,
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.data.length == 0) {

            that.setData({
              gdMessage: []
            })
          } else {
            let arr = [];
            that.setData({
              gdMessage: res.data.data
            })


            wx.showTabBarRedDot({
              index: 1,
            })
          }
        } else {
          wx.showToast({
            title: '消息获取失败',
            icon: "none"
          })
        }
      }
    })
  },
  messageAction: function() {
    wx.hideTabBarRedDot({
      index: 1,
    })
    wx.navigateTo({
      url: '../../gjIndex/msgList/msgList?type=2',
    })
  }
})